/*
 * tempfilter.c
 * Asif Shahidullah
 *
 * Contains the main loop for the tempfilter program. It follows as:
 * 	Open input file;
 * 	Construct initial data list;
 * 	Remove erroneous data points;
 * 	Apply low-pass filter;
 * 	Extract data trend;
 * 	Write trend data to specified file;
 * 	Deinitialize all allocated memory.
 */

#include <stdio.h>
#include <stdlib.h>

#include "d_list.h"
#include "filter.h"
#include "fout.h"
#include "fread.h"
#include "trender.h"

/* Quit program with specified code and message (printed to
 * stderr.) */
void die(int code, char *message) {
	fprintf (stderr, message);
	exit (code);
}

int main (int argc, char **argv) {
	List data_list;

	if (argc != 3) {
		fprintf (stderr, "Usage: %s <inputFile> <outputFile>\n", *argv);
		return EXIT_FAILURE;
	}

	if ((data_list = allocate_list()) == NULL)
		die (EXIT_FAILURE, "");

	if (! create_list (data_list, *(argv+1)))
		die(EXIT_FAILURE, "");

	if (data_list->size == 0)
		die(EXIT_FAILURE, "Empty file passed, terminating here.\n");

	remove_err_data (data_list);

	attenuate_data (data_list);

	analyze_trend (data_list);

	if (write_output (data_list, *(argv+2)) < 0)
		die (EXIT_FAILURE, "");

	deinit_list (data_list);
	
	return 0;

}
